//动画路线颜色
var headColors = ['#ECFFB1', '#146968', '#146968', '#146968', '#146968', '#146968', '#146968', '#146968'];
var trailColors = [
    'rgba(255,178,6, 0.2)',
    'rgba(255,178,6, 0.2)',
    'rgba(20,105,104, 0.2)',
    'rgba(20,105,104, 0.2)',
    'rgba(20,105,104, 0.2)',
    'rgba(20,105,104, 0.2)',
    'rgba(20,105,104, 0.2)',
    'rgba(20,105,104, 0.2)',
];

/**
 * 创建导航地图
 * @param map
 * @private
 */
export function _createRoadLine(map) {
    var driving = new AMap.TruckDriving({
        map: map,
        policy:0,
        size:1,
        extensions:'all',
        hideMarkers:true,
    });
    var path = [];
    path.push({lnglat:[106.56288, 29.556742]});//起点
    path.push({lnglat:[106.61288, 29.556742]});//途径
    path.push({lnglat:[106.56288, 29.596742]});//终点
    driving.search(path,function(status, result) {
        console.log(status)
        if(status =="complete"){
            console.log(result.routes[0].steps)
        }

    })
}

/**
 * 路线动画 （暂时vue不可用）
 * @param map
 * @private
 */
export function _createDiyLine(map) {
    AMapUI.load(['ui/misc/PathSimplifier', 'lib/$'], function(PathSimplifier, $) {

        if (!PathSimplifier.supportCanvas) {
            alert('当前环境不支持 Canvas！');
            return;
        }

        var pathSimplifierIns = new PathSimplifier({
            zIndex: 100,
            //autoSetFitView:false,
            map: map, //所属的地图实例

            getPath: function(pathData, pathIndex) {

                return pathData.path;
            },
            getHoverTitle: function(pathData, pathIndex, pointIndex) {

                if (pointIndex >= 0) {
                    //point
                    return pathData.name + '，点：' + pointIndex + '/' + pathData.path.length;
                }

                return pathData.name + '，点数量' + pathData.path.length;
            },
            renderOptions: {

                renderAllPointsIfNumberBelow: 100 //绘制路线节点，如不需要可设置为-1
            }
        });

        window.pathSimplifierIns = pathSimplifierIns;

        //设置数据
        pathSimplifierIns.setData([{
            name: '路线0',
            path: [
                [106.56288, 29.556742],
                [106.61288, 29.556742],

            ]
        }]);

        //对第一条线路（即索引 0）创建一个巡航器
        var navg1 = pathSimplifierIns.createPathNavigator(0, {
            loop: true, //循环播放
            speed: 10000 //巡航速度，单位千米/小时
        });

        navg1.start();
    });
}

/**
 * 供热需求呼吸点
 * @param data
 * @param loca
 * @param map
 * @private
 */
export function _createInPointTrigger(data,loca,map) {
    //需要排放热能
    var geoLevelF = new Loca.GeoJSONSource({
        data: data
    });

    var breathRed = new Loca.ScatterLayer({
        loca:loca,
        zIndex: 113,
        opacity: 1,
        visible: true,
        zooms: [2, 22],
    });
    breathRed.setSource(geoLevelF);

    breathRed.setStyle({
        unit: 'px',
        size: [26, 26],
        borderWidth: 0,
        texture: 'https://a.amap.com/Loca/static/loca-v2/demos/images/breath_red.png',
        duration: 500,
        animate: true,
    });
}

/**
 * 需热呼吸点
 * @private
 */
export function _createOutPointTrigger(data,loca,map) {
    // 需要供应热能
    var geoLevelE = new Loca.GeoJSONSource({
        data: data
    });

    var breathYellow = new Loca.ScatterLayer({
        loca:loca,
        zIndex: 112,
        opacity: 1,
        visible: true,
        zooms: [2, 22],
    });
    breathYellow.setSource(geoLevelE);
    breathYellow.setStyle({
        unit: 'px',
        size: [25, 25],
        borderWidth: 0,
        texture: 'https://a.amap.com/Loca/static/loca-v2/demos/images/breath_yellow.png',
        duration: 1000,
        animate: true,
    });
}

/**
 * 供热动画路线
 * @param data
 * @param loca
 * @param map
 * @private
 */
export function _createInLine(data,loca,map) {
    // 送热路线
    var inLineSource = new Loca.GeoJSONSource({
        data:{
            "type": "FeatureCollection",
            "features": [
              data
            ]
        }
    });

    var inLinelayer = new Loca.PulseLineLayer({
        loca:loca,
        zIndex: 11,
        opacity: 1,
        visible: true,
        zooms: [2, 22],
    });

    inLinelayer.setStyle({
        altitude: 0,
        lineWidth: (_, feature) => feature.properties.lineWidthRatio * 4 + 1,
        headColor: (_, feature) => headColors[feature.properties.type],
        trailColor: (_, feature) => trailColors[feature.properties.type],
        interval: 0.5,
        duration: 2000,
    });
    inLinelayer.setSource(inLineSource);

}

var outLinelayer = null
/**
 * 需热动画路线
 * @private
 */
export function _createOutLine(data,loca,map) {

  console.log('供热路线',data)

    // 供热路线
    var outLineSource = new Loca.GeoJSONSource({
        data:data
    });


    if(!outLinelayer){
      outLinelayer =new Loca.PulseLineLayer({
        loca:loca,
        zIndex: 11,
        opacity: 1,
        visible: true,
        zooms: [2, 22],
      });
      outLinelayer.setStyle({
        altitude: 0,
        lineWidth: (_, feature) => feature.properties.lineWidthRatio * 1 + 3,
        headColor: (_, feature) => headColors[feature.properties.type],
        trailColor: (_, feature) => trailColors[feature.properties.type],
        interval: 0.25,
        duration: 5000,
      });
    }
  outLinelayer.setSource(outLineSource);
}
